package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.application.query.vo.MajorHazardVo;
import com.smedi.ismedi.inmail.core.domain.dto.MajorHazardQuery;
import com.smedi.ismedi.inmail.core.domain.dto.TimeLineQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.MajorHazardRemoteApi;
import com.smedi.ismedi.majorhazard.api.dto.request.MajorHazardListRequest;
import com.smedi.ismedi.majorhazard.api.dto.request.MajorHazardTimeLineRequest;
import com.smedi.ismedi.majorhazard.api.dto.response.QueryMajorHazardListResponse;
import com.smedi.ismedi.majorhazard.api.feign.RemoteMajorHazardFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
@RequiredArgsConstructor
@Service
public class MajorHazardRemoteApiImpl implements MajorHazardRemoteApi {
    private final RemoteMajorHazardFeignApi remoteMajorHazardFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public List<MajorHazardVo> getMajorHazardList(MajorHazardQuery majorHazardQuery) {
        MajorHazardListRequest majorHazardListRequest = adapterConverter.majorHazardQueryToMajorHazardListRequest(majorHazardQuery);
        ResponseData<List<QueryMajorHazardListResponse>> responseData = remoteMajorHazardFeignApi.getMajorHazardList(majorHazardListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.majorHazardResponseToMajorHazard(responseData.getData());
    }

    @Override
    public Integer getMajorHazardCount(MajorHazardQuery majorHazardQuery) {
        MajorHazardListRequest majorHazardListRequest = adapterConverter.majorHazardQueryToMajorHazardListRequest(majorHazardQuery);
        ResponseData<Integer> responseData = remoteMajorHazardFeignApi.getMajorHazardCount(majorHazardListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }

    @Override
    public Integer getMajorHazardCountByTimeLine(TimeLineQuery timeLineQuery) {
        MajorHazardTimeLineRequest timeLineRequest = adapterConverter.timeLineQueryToMajorHazardTimeLineRequest(timeLineQuery);
        ResponseData<Integer> responseData = remoteMajorHazardFeignApi.getMajorHazardCountByTimeLine(timeLineRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }

}
